var e = require;
var t = module;
var o = exports;
var ii, nn = this && this.__extends || (ii = function (t, e) {
    return (ii = Object.setPrototypeOf || {
        __proto__: []
    }
        instanceof Array &&
        function (t, e) {
            t.__proto__ = e
        } ||
        function (t, e) {
            for (var o in e) Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o])
        })(t, e)
},
    function (t, e) {
        function o() {
            this.constructor = t
        }
        ii(t, e),
            t.prototype = null === e ? Object.create(e) : (o.prototype = e.prototype, new o)
    }
),
    rr = this && this.__awaiter ||
        function (t, e, o, i) {
            return new (o || (o = Promise))(function (n, a) {
                function r(t) {
                    try {
                        l(i.next(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function s(t) {
                    try {
                        l(i.
                            throw(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function l(t) {
                    var e;
                    t.done ? n(t.value) : (e = t.value, e instanceof o ? e : new o(function (t) {
                        t(e)
                    })).then(r, s)
                }
                l((i = i.apply(t, e || [])).next())
            })
        };
var oo = (this && this.__decorate) ||
    function (e, t, a, n) {
        var i,
            o = arguments.length,
            s = o < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, a)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) s = Reflect.decorate(e, t, a, n);
        else
            for (var r = e.length - 1; r >= 0; r--)
                (i = e[r]) && (s = (o < 3 ? i(s) : o > 3 ? i(t, a, s) : i(t, a)) || s);
        return o > 3 && s && Object.defineProperty(t, a, s), s;
    };
Object.defineProperty(o, "__esModule", { value: !0 });
var a = cc._decorator,
    n = a.ccclass,
    i =
        (a.property,
            (function () {
                function e() { }
                return (
                    (e.showProgressBar = function (e) {
                        void 0 === e && (e = 0), this.showMask();
                    }),
                    (e.updateProgress = function (e, t) {
                        var o = e / t;
                        o > 1 && (o = 1);
                    }),
                    (e.hideProgressBar = function (e) {
                        void 0 === e && (e = 1), this.hideMask(e);
                    }),
                    (e.showMask = function (e) {
                        void 0 === e && (e = 1);
                    }),
                    (e.hideMask = function (e) {
                        void 0 === e && (e = 1);
                    }),
                    (e.loadBundle = function (e, t, o, a) {
                        void 0 === o && (o = !1), void 0 === a && (a = !1), this.loadSubpackage(e, t, o, a);
                    }),
                    (e.loadSubpackage = function (e, t, o, a) {
                        void 0 === o && (o = !1), void 0 === a && (a = !1);
                        var n = this.subpackageRecords[e];
                        switch ((n || ((n = new s(e, t, o)), (this.subpackageRecords[e] = n)), n.state)) {
                            case r.inited:
                                o && this.showSubpackageProgress(),
                                    a && this.subpackageSequence.length > 0
                                        ? (this.subpackageSequence.splice(1, 0, e), n.enterSequence())
                                        : (this.subpackageSequence.push(e),
                                            this.subpackageSequence.length > 1 ? n.enterSequence() : this._loadSubpackage(e));
                                break;
                            case r.waiting:
                                if (
                                    (o && this.showSubpackageProgress(),
                                        n.pushCb(t, o),
                                        a && this.subpackageSequence.length > 0)
                                ) {
                                    var i = this.subpackageSequence.indexOf(e);
                                    i > 1 &&
                                        (this.subpackageSequence.splice(i, 1),
                                            this.subpackageSequence.splice(1, 0, e),
                                            n.enterSequence());
                                }
                                break;
                            case r.turnTo:
                                o && this.showSubpackageProgress(), n.pushCb(t, o), this._loadSubpackage(e);
                                break;
                            case r.loading:
                                o && this.showSubpackageProgress(), n.pushCb(t, o);
                                break;
                            case r.finished:
                                setTimeout(function () {
                                    t && t();
                                }, 0);
                        }
                    }),
                    (e._loadSubpackage = function (e) {
                        var t = this;
                        this.subpackageRecords[e].loadStart(),
                            cc.assetManager.loadBundle(e, function (o) {
                                if (o) return console.error("子包加载出错：", e), void console.error(o);
                                console.log("子包加载完成：", e);
                                var a = t.subpackageSequence.indexOf(e);
                                if (
                                    (t.subpackageSequence.splice(a, 1),
                                        t.hideSubpackageProgress(),
                                        t.subpackageRecords[e].loadFinish(),
                                        t.subpackageSequence.length > 0)
                                ) {
                                    var n = t.subpackageSequence[0],
                                        i = t.subpackageRecords[n];
                                    i && i.turnToLoad(), t.loadSubpackage(n, null, !!t.subpackageRecords[n].maskCount);
                                }
                            });
                    }),
                    (e.showSubpackageProgress = function () {
                        null === this.subpackageProgressTimer && (this.showProgressBar(), (this.subpackageProgress = 0));
                    }),
                    (e.updateSubpackageProgress = function () {
                        (this.subpackageProgress += 0.03), this.subpackageProgress >= 1 && (this.subpackageProgress = 0);
                    }),
                    (e.hideSubpackageProgress = function () {
                        if (null !== this.subpackageProgressTimer) {
                            for (var e = 0, t = this.subpackageSequence.length - 1; t >= 0; --t)
                                e += this.subpackageRecords[this.subpackageSequence[t]].maskCount;
                            0 == e &&
                                (clearInterval(this.subpackageProgressTimer),
                                    (this.subpackageProgressTimer = null),
                                    (this.subpackageProgress = 0),
                                    this.hideProgressBar());
                        }
                    }),
                    (e.loadBundleRes = function (e, t, o, a, n) {
                        var i = this,
                            r = cc.assetManager.getBundle(e);
                        if (!r) return console.error("资源包 " + e + " 尚未加载，无法获取资源:", t), void o(null);
                        var s = null;
                        void 0 === a ? (n = !0) : "boolean" == typeof a ? (n = !!a) : ((s = a), void 0 === n && (n = !0)),
                            n && this.showMask(),
                            null !== s
                                ? r.load(t, s, function (e, t) {
                                    if ((n && i.hideMask(), e)) return console.error(e.message || e), void o(null);
                                    o(t);
                                })
                                : r.load(t, function (e, t) {
                                    if ((n && i.hideMask(), e)) return console.error(e.message || e), void o(null);
                                    o(t);
                                });
                    }),
                    (e.loadBundleArray = function (e, t, o, a, n) {
                        var i = this,
                            r = cc.assetManager.getBundle(e);
                        if (!r) return console.error("资源包 " + e + " 尚未加载，无法获取资源数组:", t), void o(null);
                        var s = null;
                        void 0 === a ? (n = !0) : "boolean" == typeof a ? (n = !!a) : ((s = a), void 0 === n && (n = !0)),
                            n && this.showProgressBar(),
                            s
                                ? r.load(t, s, this.updateProgress.bind(this), function (e, t) {
                                    if ((n && i.hideProgressBar(), e)) return console.error(e.message || e), void o(null);
                                    o(t);
                                })
                                : r.load(t, this.updateProgress.bind(this), function (e, t) {
                                    if ((n && i.hideProgressBar(), e)) return console.error(e.message || e), void o(null);
                                    o(t);
                                });
                    }),
                    (e.loadBundleDir = function (e, t, o, a, n) {
                        var i = this,
                            r = cc.assetManager.getBundle(e);
                        if (!r) return console.error("资源包 " + e + " 尚未加载，无法获取资源文件夹:", t), void o(null);
                        var s = null;
                        void 0 === a ? (n = !0) : "boolean" == typeof a ? (n = !!a) : ((s = a), void 0 === n && (n = !0)),
                            n && this.showProgressBar(),
                            s
                                ? r.loadDir(t, s, this.updateProgress.bind(this), function (e, t) {
                                    if ((n && i.hideProgressBar(), e)) return console.log(e), void o(null);
                                    o(t);
                                })
                                : r.loadDir(t, this.updateProgress.bind(this), function (e, t) {
                                    if ((n && i.hideProgressBar(), e)) return console.log(e), void o(null);
                                    o(t);
                                });
                    }),
                    (e.loadBundleScene = function (e, t, o, a) {
                        var n = this,
                            i = cc.assetManager.getBundle(e);
                        if (!i) return console.error("资源包 " + e + " 尚未加载，无法加载场景:", t), void o(null);
                        void 0 === a && (a = !0),
                            a && this.showProgressBar(),
                            i.loadScene(t, this.updateProgress.bind(this), function (e, t) {
                                a && n.hideProgressBar(), e ? console.error(e) : o(t);
                            });
                    }),
                    (e.preLoadBundleRes = function (e, t, o) {
                        var a = cc.assetManager.getBundle(e);
                        a && (void 0 === o ? a.preload(t) : a.preload(t, o));
                    }),
                    (e.preLoadBundleArray = function (e, t, o) {
                        var a = cc.assetManager.getBundle(e);
                        a && (void 0 === o ? a.preload(t) : a.preload(t, o));
                    }),
                    (e.preLoadBundleDir = function (e, t, o) {
                        var a = cc.assetManager.getBundle(e);
                        a && (void 0 === o ? a.preloadDir(t) : a.preloadDir(t, o));
                    }),
                    (e.preLoadBundleScene = function (e, t) {
                        var o = cc.assetManager.getBundle(e);
                        o && o.preloadScene(t);
                    }),
                    (e.dirAsset = {}),
                    (e.singleAsset = {}),
                    (e.subpackageRecords = {}),
                    (e.subpackageSequence = []),
                    (e.subpackageProgressTimer = null),
                    (e.subpackageProgress = 0),
                    oo([n("Loader")], e)
                );
            })());
o.default = i;
var r,
    s = (function () {
        function e(e, t, o) {
            (this.name = e),
                (this.state = r.inited),
                (this.cbs = []),
                t && this.pushCb(t),
                (this.maskCount = o ? 1 : 0);
        }
        return (
            (e.prototype.pushCb = function (e, t) {
                e && this.cbs.push(e), t && this.maskCount++;
            }),
            (e.prototype.enterSequence = function () {
                this.state = r.waiting;
            }),
            (e.prototype.loadStart = function () {
                this.state = r.loading;
            }),
            (e.prototype.loadFinish = function () {
                for (; this.cbs.length > 0;) {
                    var e = this.cbs.shift();
                    e && e();
                }
                (this.state = r.finished), (this.maskCount = 0);
            }),
            (e.prototype.turnToLoad = function () {
                this.state = r.turnTo;
            }),
            e
        );
    })();
(function (e) {
    (e[(e.inited = 1)] = "inited"),
        (e[(e.waiting = 2)] = "waiting"),
        (e[(e.loading = 3)] = "loading"),
        (e[(e.finished = 4)] = "finished"),
        (e[(e.turnTo = 5)] = "turnTo");
})(r || (r = {}));
